Namespace Utility

    ''' <summary>
    ''' iLinePro language service.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class LanguageService
        Inherits Object

        Private m_Languages As ArrayList
        Private m_Language As Collections.DictionaryEntry
        Private m_Replace, m_Newline As String
        Private m_xnLang As Xml.XmlNode
        Private m_fileName As String
        Private m_miLangs As ArrayList

        ''' <summary>
        ''' In message string the new-line replace char.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Newline() As String
            Get
                Return Me.m_Newline
            End Get
        End Property

        ''' <summary>
        ''' In message string the replace char.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Replace() As String
            Get
                Return Me.m_Replace
            End Get
        End Property

        ''' <summary>
        ''' Crrent language name.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Language() As String
            Get
                Return Me.m_Language.Key
            End Get
            Set(ByVal Value As String)
                Dim de As Collections.DictionaryEntry
                Dim blnExist As Boolean = False
                For Each de In Me.m_Languages
                    If de.Key = Value Then
                        blnExist = True
                        Exit For
                    End If
                Next
                If blnExist = True Then
                    Me.m_Language = de
                Else
                    Throw New InvalidateLanguageNameException(Value)
                End If
            End Set
        End Property

        ''' <summary>
        ''' Item in current language list by item name.
        ''' </summary>
        ''' <param name="Name">Item name</param>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Item(ByVal Name As String) As String
            Get
                Dim ht As Hashtable
                If Me.m_Language.Key = "" Then
                    Throw New InvalidateItemNameException("[EMPTY]", Name)
                Else
                    ht = Me.m_Language.Value
                    If ht.ContainsKey(Name) = True Then
                        Return ht.Item(Name)
                    Else
                        Throw New InvalidateItemNameException(Me.m_Language.Key, Name)
                    End If
                End If
            End Get
        End Property

        ''' <summary>
        ''' Menu items that created in main form loaded from xml.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property MenuItems() As LanguageMenuItem()
            Get
                Return Me.m_miLangs.ToArray(GetType(LanguageMenuItem))
            End Get
        End Property

        ''' <summary>
        ''' Create instance.
        ''' </summary>
        ''' <param name="xmlLan">Language.xml file name (with path).</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal xmlLan As String)
            Dim docLan As New Xml.XmlDocument
            Try
                Me.m_Replace = "@"
                Me.m_Newline = "#"
                Me.m_Languages = New ArrayList
                Me.m_miLangs = New ArrayList
                If IO.File.Exists(xmlLan) = True Then
                    Me.m_fileName = xmlLan
                    docLan.Load(xmlLan)
                    Me.Load(docLan)
                End If
            Catch ex As Exception
            End Try
        End Sub

        ''' <summary>
        ''' Save the current language name by user selected.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Save()
            Dim attrib As Xml.XmlAttribute
            If Not Me.m_xnLang Is Nothing Then
                attrib = Me.m_xnLang.Attributes("value")
                If Not attrib Is Nothing AndAlso attrib.Value <> Me.Language Then
                    attrib.Value = Me.Language
                    attrib.OwnerDocument.Save(Me.m_fileName)
                End If
            End If
        End Sub

        ''' <summary>
        ''' Load the language information from language DOM.
        ''' </summary>
        ''' <param name="docLan">Language xml DOM.</param>
        ''' <remarks></remarks>
        Private Sub Load(ByVal docLan As Xml.XmlDocument)
            Dim xn, _xn As Xml.XmlNode
            Dim xns As Xml.XmlNodeList
            Dim de As Collections.DictionaryEntry
            Dim curLang As String = String.Empty
            'Load the setting information
            xn = docLan.SelectSingleNode("//Languages/Settings")
            If xn IsNot Nothing Then
                curLang = Me.LoadSetting(xn)
            End If
            'Load language information
            xns = docLan.SelectNodes("//Languages/Language")
            For Each _xn In xns
                de = Me.LoadLanguage(_xn)
                Me.m_Languages.Add(de)
                Me.m_miLangs.Add(New LanguageMenuItem(de.Key))
            Next
            'Set current language
            If curLang IsNot Nothing AndAlso curLang.Length > 0 Then
                Me.Language = curLang
            End If
        End Sub

        ''' <summary>
        ''' Load the language setting information.
        ''' </summary>
        ''' <param name="xnSetting">Setting xml node.</param>
        ''' <returns>Current language name.</returns>
        ''' <remarks></remarks>
        Private Function LoadSetting(ByVal xnSetting As Xml.XmlNode) As String
            Dim xns As Xml.XmlNodeList = xnSetting.SelectNodes("Setting")
            Dim xn As Xml.XmlNode
            Dim curLang As String = String.Empty
            For Each xn In xns
                Select Case xn.Attributes("name").Value
                    Case "REPLACE"
                        Me.m_Replace = xn.Attributes("value").Value
                    Case "NEWLINE"
                        Me.m_Newline = xn.Attributes("value").Value
                    Case "LANG"
                        Me.m_xnLang = xn
                        curLang = xn.Attributes("value").Value
                End Select
            Next
            Return curLang
        End Function

        ''' <summary>
        ''' Load the language information.
        ''' </summary>
        ''' <param name="xnLanguage">Language xml node.</param>
        ''' <returns>Language information.</returns>
        ''' <remarks></remarks>
        Private Function LoadLanguage(ByVal xnLanguage As Xml.XmlNode) As Collections.DictionaryEntry
            Dim ht As Hashtable
            Dim de As Collections.DictionaryEntry
            ht = Me.LoadLanguageInfo(xnLanguage)
            de = New Collections.DictionaryEntry(xnLanguage.Attributes("name").Value, ht)
            Return de
        End Function

        ''' <summary>
        ''' Load the language items information.
        ''' </summary>
        ''' <param name="xnLan">Language items xml node.</param>
        ''' <returns>Language information.</returns>
        ''' <remarks></remarks>
        Private Function LoadLanguageInfo(ByVal xnLan As Xml.XmlNode) As Hashtable
            Dim ht As New Hashtable
            'Load common infomation
            Me.LoadLanguageInfo(xnLan, "Common", ht)
            'Load message infomation
            Me.LoadLanguageInfo(xnLan, "Message", ht)
            'Load platform infomation
            Me.LoadLanguageInfo(xnLan, "Platform", ht)
            'Load language menu item information
            Me.LoadLanguageInfo(xnLan, "MenuLang", ht)
            'Load about form information
            Me.LoadLanguageInfo(xnLan, "About", ht)
            Return ht
        End Function

        '------------------------------------------------------------
        '[NAME] LoadLanguageInfo
        '[PROP] Private
        '[FUNC] Load the language sub-items information.
        '[PARA] ByVal xnLan As Xml.XmlNode: Language items xml node.
        '       ByVal Kind As String: Infomation kind.
        '       ByRef ht As Hashtable: Language information.
        '[RETN] 
        '[AUTH] Xuzy
        '[DATE] 2004-06-29
        '------------------------------------------------------------
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="xnLan"></param>
        ''' <param name="Kind"></param>
        ''' <param name="ht"></param>
        ''' <remarks></remarks>
        Private Sub LoadLanguageInfo(ByVal xnLan As Xml.XmlNode, ByVal Kind As String, ByRef ht As Hashtable)
            Dim xn, _xn As Xml.XmlNode
            Dim xns As Xml.XmlNodeList
            Dim val As String
            xn = xnLan.SelectSingleNode("Items [@kind = '" & Kind & "']")
            If Not xn Is Nothing Then
                xns = xn.SelectNodes("Item")
                For Each _xn In xns
                    If ht.ContainsKey(_xn.Attributes("name").Value) = False Then
                        val = _xn.Attributes("value").Value.Replace(Me.m_Newline, vbNewLine)
                        ht.Add(_xn.Attributes("name").Value, val)
                    End If
                Next
            End If
        End Sub

        '------------------------------------------------------------
        '[NAME] ShowMessage
        '[PROP] Private
        '[FUNC] Display the message box by language item.
        '[PARA] ByVal Name As String: Language item name to display.
        '       ByVal Style As MsgBoxStyle: Style of message box.
        '       Optional ByVal Replace As String = "": Replace string.
        '[RETN] DialogResult: Message box result.
        '[AUTH] Xuzy
        '[DATE] 2004-06-29
        '------------------------------------------------------------
        Public Function ShowMessage(ByVal Name As String, ByVal Style As MsgBoxStyle, ByVal Title As String, Optional ByVal Replace As String = "") As Windows.Forms.DialogResult
            Dim ret As Windows.Forms.DialogResult
            Dim prop As String
            Try
                prop = Me.Item(Name)
                prop = prop.Replace(Me.m_Replace, Replace)
                ret = MsgBox(prop, Style, Title)
            Catch ex As Exception
                ret = Windows.Forms.DialogResult.Cancel
            End Try
            Return ret
        End Function

#Region " Exceptions "

        '------------------------------------------------------------
        '[NAME] InvalidateLanguageNameException
        '[PROP] Public, Exception
        '[FUNC] Invalidate language name(key) exception
        '[AUTH] Xuzy
        '[DATE] 2004-06-29
        '------------------------------------------------------------
        Public Class InvalidateLanguageNameException
            Inherits Exception

            '------------------------------------------------------------
            '[NAME] Create instance.
            '[PROP] Public
            '[FUNC] Create instance.
            '[PARA] ByVal LanguageName As String: Language name.
            '[RETN] 
            '[AUTH] Xuzy
            '[DATE] 2004-06-29
            '------------------------------------------------------------
            Public Sub New(ByVal LanguageName As String)
                MyBase.New(LanguageName & " language information was not statmented in Language.xml." & vbCrLf & "Pleas reset Langeag.xml and try again.")
            End Sub

        End Class

        '------------------------------------------------------------
        '[NAME] InvalidateItemNameException
        '[PROP] Public, Exception
        '[FUNC] Invalidate item name(key) exception
        '[AUTH] Xuzy
        '[DATE] 2004-06-29
        '------------------------------------------------------------
        Public Class InvalidateItemNameException
            Inherits Exception

            '------------------------------------------------------------
            '[NAME] Create instance.
            '[PROP] Public
            '[FUNC] Create instance.
            '[PARA] ByVal LanguageName As String: Language name.
            '       ByVal ItemName As String: Item name.
            '[RETN] 
            '[AUTH] Xuzy
            '[DATE] 2004-06-29
            '------------------------------------------------------------
            Public Sub New(ByVal LanguageName As String, ByVal ItemName As String)
                MyBase.New(ItemName & " item name in " & LanguageName & " language information was not statmented in Language.xml." & vbCrLf & "Pleas reset Langeag.xml and try again.")
            End Sub

        End Class

#End Region

#Region " Language MenuItem "

        '------------------------------------------------------------
        '[NAME] LanguageMenuItem
        '[PROP] Public, MenuItem, ICloneable
        '[FUNC] Language menu item create in main form.
        '[AUTH] Xuzy
        '[DATE] 2004-06-29
        '------------------------------------------------------------
        Public Class LanguageMenuItem
            Inherits Windows.Forms.MenuItem
            Implements ICloneable

            Private m_Lang As String

            Public Event LanguageChanged(ByVal sender As Object, ByVal Language As String)

            '------------------------------------------------------------
            '[NAME] Language
            '[PROP] Public
            '[FUNC] Create instance.
            '[PARA] 
            '[RETN] String: Current menu's language value.
            '[AUTH] Xuzy
            '[DATE] 2004-06-29
            '------------------------------------------------------------
            Public ReadOnly Property Language() As String
                Get
                    Return Me.m_Lang
                End Get
            End Property

            '------------------------------------------------------------
            '[NAME] Create instance.
            '[PROP] Public
            '[FUNC] Create instance.
            '[PARA] ByVal Lang As String: Language name.
            '[RETN] 
            '[AUTH] Xuzy
            '[DATE] 2004-06-29
            '------------------------------------------------------------
            Public Sub New(ByVal Lang As String)
                MyBase.New()
                Me.m_Lang = Lang
            End Sub

            '------------------------------------------------------------
            '[NAME] OnClick
            '[PROP] Protected, Overrides
            '[FUNC] On click event, raise LanguageChanged event.
            '[PARA] 
            '[RETN] 
            '[AUTH] Xuzy
            '[DATE] 2004-06-29
            '------------------------------------------------------------
            Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
                MyBase.OnClick(e)
                RaiseEvent LanguageChanged(Me, Me.m_Lang)
            End Sub

            Public Function Clone() As Object Implements System.ICloneable.Clone
                Return New LanguageMenuItem(Me.m_Lang)
            End Function

        End Class

#End Region

#Region " Interface "

        '------------------------------------------------------------
        '[NAME] ILanguageService
        '[PROP] 
        '[FUNC] Change language interface.
        '[AUTH] Xuzy
        '[DATE] 2004-06-27
        '------------------------------------------------------------
        Public Interface ILanguageService

            '------------------------------------------------------------
            '[NAME] ChangeLanguage
            '[PROP] 
            '[FUNC] Change language abstract function..
            '[PARA] 
            '[RETN] 
            '[AUTH] Xuzy
            '[DATE] 2004-06-27
            '------------------------------------------------------------
            Sub ChangeLanguage()

        End Interface

#End Region

    End Class

End Namespace
